$(function () {
    $("#inputdiv:not('span.zspan')").on("click", function () {
        console.log("on");
        $(this).find("input").show();
        $(this).find("input").focus();
    })
    document.addEventListener("click", function (e) {

        if (e.target.id != "inputdiv" || e.target.id != "tellinp") {
            console.log("off");
        }
    }, true);

    $("#inputdiv").on("click", "a.zspan-btn", function () {
        delZspan();
        // $(this).parent().hide();
    });

    //监听输入事件，获取输入值
    $("input#tellinp").on("input propertychange", function () {
        $sosoText = $(this).val();
        if ($sosoText.length == 0) {
            $(this).keydown(function (event) {
                console.log(event.which);
                if (event.which == 8) {

                }
            });
        }
    });

    //监听键盘事件1.enter搜索
    $("input#tellinp").keydown(function (event) {
        console.log(event.which);
        $sosoText = $(this).val();
        if (event.which == 8 && $sosoText.length == 0) {
            delZspan();
        } else if (event.which == 13 && $sosoText.length != 0) {
            bronZspan($sosoText);
        } else if (event.which == 32 && $sosoText.length != 0) {
            bronZspan($sosoText);
        } else if (event.which == 186 && $sosoText.length != 0) {
            bronZspan($sosoText);
        }
    });

    $("input#tellinp").blur(function (event) {
        console.log(event.which);
        $sosoText = $(this).val().trim();
        var testtell = /^[0-9]{11,13}$/;
        if ($sosoText.length != 0) {
            var bool = testtell.test($sosoText);
            bronZspan($sosoText, bool);
        }

    });

    //生成新的span，
    function bronZspan(txt, bool) {
        //num=1代表
        var $span;
        if (bool) {
            $span = "<span class='zspan zspan-yes'>" + txt + "</span>";
        } else {
            $span = "<span class='zspan zspan-no'>" + txt + "<a class='zspan-btn'>x</a></span>";
        }
        $("input#tellinp").before($span);
        $("input#tellinp").val("");
    }

    //删除已有span
    function delZspan() {
        var list = document.getElementById("inputdiv");
        var spanlist = list.getElementsByTagName("span")
        var spanlist = $("#inputdiv").find("span");
        var spanlistnum = spanlist.length;
        list.removeChild(spanlist[spanlistnum - 1]);
        // $("#inputdiv").find("span").eq(spanlistnum - 1).empty();
        // $("#inputdiv").find("span").eq(spanlistnum - 1).html("");
        // $("#inputdiv").find("span").eq(spanlistnum - 1).remove();
    }

    //常用语效果
    $(".zbtn").on("click", function () {
        $("ul.ullist").slideToggle(800);
    });

    //选择常用语
    $("ul.ullist").on("click", "li", function () {
        var txt = $(this).text();
        console.log(txt);
        $("#scont").val(txt);
        $(this).parent().slideToggle(800);
    })
    //
    //发送短信
    $(".zsend").click(function () {
        var yspan = $("#inputdiv").find(".zspan-yes");
        var tell = [];
        for (var i = 0; i < yspan.length; i++) {
            var temp = yspan.eq(i).text();
            var tempnum = temp.length;
            tell.push(temp.substring(0, tempnum));
        }

        var nspan = $("#inputdiv").find(".zspan-no");
        for (var i = 0; i < nspan.length; i++) {
            var temp = nspan.eq(i).text();
            var tempnum = temp.length;
            tell.push(temp.substring(0, tempnum - 1));
        }

        console.log(tell);
        var tellStr = tell.join(",");
        var usercontent = $("#scont").val();
        if (tell.length == 0 || usercontent == "" || usercontent.length == 0) {
            showTips(0);
            return;
        } else {
            var sendtxt = encodeURI(encodeURI(usercontent));
            sendAjax("smspocessor/doSend.do", tellStr, sendtxt);
        }
    });

    function setTxt(txt) {
        $(".zalert").css('visibility', "visible");
        $(".zalert").find("p#msg").text(txt);
        $(".zalert").fadeIn(1300);
    }

    //关闭提示框
    $(".zzclose").click(function () {
        $(".zalert").find("p#msg").text(" ");
        $(".zalert").fadeOut();
        setTimeout(function () {
            $(".zalert").css('visibility', "hidden");
        }, 1000);
    })

    $(".tipsclose").click(function () {
        $(".tips").animate({
            top: "-10px"
        });
        $(".tips").css("display", "none");
    })
    //关闭提示框

    function showTips(num) {
        $("div.tips").css("display", "block");
        $("div.tips").show();
        if (num == 1) {
            $(".tips").find("p").html("&radic; 短信已发成功！")
            $("div.tips").addClass("tipsuc");
        } else {
            $(".tips").find("p").html("! 您未填写重要信息，如：物联卡号，内容")
            $("div.tips").removeClass("tipsuc");
        }

        $("div.tips").animate({
            top: "3px"
        });
        setTimeout(function () {
           $("div.tips").hide();
        },3000);
    }

    function sendAjax(urlname, arg1, arg2) {
        // var obj = new Object();
        // obj.phone = arg1;
        // obj.content = escape(arg2);

        $.ajax({
            url: urlname,
            type: 'POST',
            data: {"phone": arg1, "content": arg2},
            success: function (data) {
                console.log(data);
                //填写信息成功，并发送成功！
                if (data.code == '0000') {
                    showTips(1);
                    return true;
                } else { //填写信息成功，但有错误！（可以添加错误原因）
                    setTxt(data.msg);
                    return false;
                }
            },
            error: function (data) {
                console.log(data);
                return false;
            }
        });
    }
})
